package com.lingshi.yingzaitownmap.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lingshi.yingzaitownmap.entity.domain.Dishes;
import com.lingshi.yingzaitownmap.entity.dto.PageDTO;
import com.lingshi.yingzaitownmap.entity.result.PageBean;
import com.lingshi.yingzaitownmap.mapper.DishesMapper;
import com.lingshi.yingzaitownmap.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishesServiceImpl implements DishesService {

    @Autowired
    private DishesMapper dishesMapper;

    /**
     * 查询美食
     *
     * @param pageDTO 分页参数
     * @return 美食列表
     */
    @Override
    public PageBean<Dishes> select(PageDTO pageDTO) {

        // 设置默认页码和每页数量
        if (pageDTO.getPage() == null || pageDTO.getPage() < 1){
            pageDTO.setPage(1);
        }
        if (pageDTO.getPageSize() == null || pageDTO.getPageSize() < 1){
            pageDTO.setPageSize(10);
        }

        // 设置分页
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        // 查询
        List<Dishes> list = dishesMapper.select(pageDTO.getKeyword());
        // 获取分页结果
        Page<Dishes> p = (Page<Dishes>) list;
        // 返回分页结果
        return new PageBean<>(p.getResult(), p.getTotal());
    }


    /**
     * 根据id查询美食
     *
     * @param id 美食id
     * @return 美食
     */
    @Override
    public Dishes getById(String id) {
        // 查询并返回结果
        return dishesMapper.getById(id);
    }

}
